<?php

namespace app\model\v1\gift;

use crmeb\traits\ModelTrait;
use think\facade\Db;
use think\Model;


class GiftCardCate extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    protected $table = 'eb_gift_card_cate';




    /**
     * 礼品卡后台 - 分类管理列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getGiftCardCateList($param = [])
    {
        $data["data"] = self::field ("*,FROM_UNIXTIME(create_time) as create_time,FROM_UNIXTIME(update_time) as update_time")
            ->whereNull("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("cate_tag_name",'like', '%'.$param["word"].'%');
                if(!empty($param["pid"])) $query->where("pid",  $param["pid"]);
            })
            ->order("id", "desc")
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->whereNull("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("cate_tag_name",'like', '%'.$param["word"].'%');
                if(!empty($param["pid"])) $query->where("pid",  $param["pid"]);
            })
            ->count();

        return $data;
    }



    public static function getGiftCardCateData($param = [])
    {
        return self::field ("id,cate_tag_name")
            ->whereNull("delete_time")
            ->where("status", 1)
            ->select()->toArray();
    }


    /**
     * 分类管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addGiftCardCate($param = [])
    {
        $add = [
            "cate_tag_name" => $param["cate_tag_name"],
            "icon"          => $param["icon"] ?? '',
            "img"           => $param["img"] ?? '',
            "mold"          => $param["mold"] ?? '',
            "head_img"      => $param["head_img"] ?? '',
            "upper_img"     => $param["upper_img"] ?? '',
            "centre_img"    => $param["centre_img"] ?? '',
            "bottom_img"    => $param["bottom_img"] ?? '',
            "bless_img"     => $param["bless_img"] ?? '',
            "img1"          => $param["img1"] ?? '',
            "img2"          => $param["img2"] ?? '',
            "img3"          => $param["img3"] ?? '',
            "img4"          => $param["img4"] ?? '',
            "img5"          => $param["img5"] ?? '',
            "img6"          => $param["img6"] ?? '',
            "img7"          => $param["img7"] ?? '',
            "img8"          => $param["img8"] ?? '',
            "img9"          => $param["img9"] ?? '',
            "img10"         => $param["img10"] ?? '',
            "sort"          => $param["sort"] ?? '',
            "create_time"   => time(),
        ];
        return self::insert($add);
    }




    /**
     * 分类管理列表 - 编辑
     * @param $param
     * @return RecruitLanguage
     */
    public static function updateGiftCardCate($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["cate_tag_name"]))      $update["cate_tag_name"] = $param["cate_tag_name"];
        if(!empty($param["sort"]))               $update["sort"] = $param["sort"]; //排序
        if(!empty($param["icon"]))               $update["icon"] = $param["icon"]; //icon
        if(!empty($param["img"]))                $update["img"] = $param["img"];
        if(!empty($param["mold"]))               $update["mold"] = $param["mold"];
        if(!empty($param["head_img"]))           $update["head_img"] = $param["head_img"];
        if(!empty($param["upper_img"]))          $update["upper_img"] = $param["upper_img"];
        if(!empty($param["centre_img"]))         $update["centre_img"] = $param["centre_img"];
        if(!empty($param["bottom_img"]))         $update["bottom_img"] = $param["bottom_img"];
        if(!empty($param["bless_img"]))          $update["bless_img"] = $param["bless_img"];
        if(!empty($param["img1"]))               $update["img1"] = $param["img1"];
        if(!empty($param["img2"]))               $update["img2"] = $param["img2"];
        if(!empty($param["img3"]))               $update["img3"] = $param["img3"];
        if(!empty($param["img4"]))               $update["img4"] = $param["img4"];
        if(!empty($param["img5"]))               $update["img5"] = $param["img5"];
        if(!empty($param["img6"]))               $update["img6"] = $param["img6"];
        if(!empty($param["img7"]))               $update["img7"] = $param["img7"];
        if(!empty($param["img8"]))               $update["img8"] = $param["img8"];
        if(!empty($param["img9"]))               $update["img9"] = $param["img9"];
        if(!empty($param["img10"]))              $update["img10"] = $param["img10"];

        return self::where("id", $param["id"])->update($update);
    }


    /**
     * 分类管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableGiftCardCate($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }





    public static function getGiftCardMold($id = '')
    {
        if(empty($id)) return [];
        return self::where("id", $id)->find() ?? [];
    }








}